package com.wjy.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjy.rbac.bean.RequestUrlBean;
import com.wjy.rbac.entity.RequestUrl;
import com.wjy.rbac.entity.RequestUrlB;
import com.wjy.rbac.mapper.RequestUrlMapper;
import com.wjy.rbac.service.IRequestUrlBService;
import com.wjy.rbac.service.IRequestUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ybxxszl
 * @since 2019-04-12
 */
@Service
public class RequestUrlServiceImpl extends ServiceImpl<RequestUrlMapper, RequestUrl> implements IRequestUrlService {

    @Autowired
    @Qualifier(value = "requestUrlBServiceImpl")
    private IRequestUrlBService requestUrlBService;

    @Override
    public void insert(RequestUrlBean requestUrlBean) {
        RequestUrl requestUrl = requestUrlBean.getRequestUrl();
        requestUrl.setStatus(0);
        save(requestUrl);
        List<RequestUrlB> requestUrlBList = requestUrlBean.getRequestUrlBList();
        for (RequestUrlB requestUrlB : requestUrlBList) {
            requestUrlB.setRequestUrlId(requestUrl.getRequestUrlId());
        }
        requestUrlBService.insert(requestUrlBList);
    }

    @Override
    public void update(RequestUrlBean requestUrlBean) {
        RequestUrl requestUrl = requestUrlBean.getRequestUrl();
        requestUrl.setStatus(0);
        updateById(requestUrl);
        requestUrlBService.deleteByRequestUrlId(requestUrl.getRequestUrlId());
        List<RequestUrlB> requestUrlBList = requestUrlBean.getRequestUrlBList();
        requestUrlBService.insert(requestUrlBList);
    }

    @Override
    public void delete(String requestUrlId) {
        removeById(requestUrlId);
        requestUrlBService.deleteByRequestUrlId(requestUrlId);
    }

    @Override
    public Map<String, Object> get(String requestUrlId) {
        Map<String, Object> result = new HashMap<String, Object>();
        RequestUrl requestUrl = getById(requestUrlId);
        result.put("requestUrl", requestUrl);
        List<RequestUrlB> requestUrlBList = requestUrlBService.getByRequestUrlId(requestUrlId);
        result.put("requestUrlBList", requestUrlBList);
        return result;
    }

    @Override
    public List<Map<String, Object>> getByRequestUrlIdList(List<String> requestUrlIdList) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (String requestUrlId : requestUrlIdList) {
            result.add(get(requestUrlId));
        }
        return result;
    }

}
